// This software is released into the Public Domain.  See copying.txt for details.
package flca.osm.perst.task;

import java.io.File;
import java.util.Map;

import org.openstreetmap.osmosis.core.container.v0_6.EntityContainer;
import org.openstreetmap.osmosis.core.task.v0_6.Sink;

import flca.osm.perst.impl.PerstWriter;
import flca.osm.perst.util.FindCornersHelper;
import flca.osm.perst.util.PerstDbHelper;
import flca.osm.perst.util.TransactionHelper;


/**
 * An OSM data sink for storing all data to an xml file.
 * 
 * @author Brett Henderson
 */
public class PerstWriterTask implements Sink {
	
	private PerstWriter osmWriter;
	
	/**
	 * Creates a new instance.
	 * 
	 * @param writer
	 *            The writer to send all data to.
	 */
	public PerstWriterTask() {
		super();
		osmWriter = new PerstWriter("osm", 0, true, false);
	}
	
	
	/**
	 * Creates a new instance.
	 * 
	 * @param file
	 *            The file to write.
	 * @param compressionMethod
	 *            Specifies the compression method to employ.
	 */
	public PerstWriterTask(File file) 
	{
		osmWriter = new PerstWriter("osm", 0, true, false);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void process(EntityContainer entityContainer) {
		
		osmWriter.process(entityContainer);
	}


	@Override
	public void release()
	{
		System.out.println("release");//TODO
	}


	@Override
	public void initialize(Map<String, Object> metaData)
	{
		PerstDbHelper.getInstance().initialize(getDbsName());
		System.out.println("initialize");//TODO
		System.out.println(metaData);
	}

	private String getDbsName() 
	{
		if (System.getProperty("os.name").toLowerCase().startsWith("l")) {
			return "/home/robin/temp/output/osm-output.db";
		} else {
			return "c:/temp/output/psm-output.db"; // TODO
		}		
	}
	
	@Override
	public void complete()
	{
		System.out.println("complete");
		FindCornersHelper.INSTANCE.updateCornerNodes();
		
		System.out.println("processed " + TransactionHelper.INSTANCE.getNodesCount() + " nodes, " +
				TransactionHelper.INSTANCE.getWaysCount() + " ways " + 
				TransactionHelper.INSTANCE.getCornerCount() + " corners");
		PerstDbHelper.getInstance().cleanup();
	}

	
}
